using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CMS.Core.DataInterfaces;
using CMS.Data;
using CMS.Data.Common;
using CMS.Core.Domain;
using CMS.Presenters;

namespace CMS.Web.ListManager
{
    public partial class LookupConfig : BaseUserControl, ILookupConfigView
    {
        private LookupConfigPresenter presenter;
        IList cList;
        Boolean IsContinues = false;
        bool isRole = false;
        NguoiSuDungInfo userInfo;

        public LookupConfig()
        {
            this.presenter = new LookupConfigPresenter(this);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter.UpdateCompleteEvent += new EventHandler(HandleUpdateCompleteEvent);
            presenter.CancelUpdateEvent += new EventHandler(HandleCancelUpdateEvent);
            presenter.DeleteCompleteEvent += new EventHandler(HandleDeleteCompleteEvent);
            if (Request.QueryString["Id"] != null)
            {
                this.presenter.InitViewForEditing(Request.QueryString.Get("Id").ToString(), false);
            }
        }

        private void HandleUpdateCompleteEvent(object sender, EventArgs e)
        {
            if (!this.IsContinues)
            {
                Response.Redirect(BaseUrlWithOnlyID + "&fName=LookupConfig");
            }
            else {
                Response.Redirect(BaseUrlWithOnlyID + "&fName=LookupConfig&action=ADD");
            }
        }

        private void HandleCancelUpdateEvent(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrlWithOnlyID + "&fName=LookupConfig");
        }

        private void HandleDeleteCompleteEvent(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrlWithOnlyID + "&fName=LookupConfig");
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            this.RaiseClientEvent();

            tblForm.Visible = (Request.QueryString["action"] != null);
            if (Request.QueryString["action"] == "EDIT") btnOKC.Visible = false;
            if (Request.QueryString["action"] != null) hrLine.Visible = true;
            userInfo = (NguoiSuDungInfo)Session["UserInfo"];
            presenter.LoadViewFromModel(Request.QueryString["action"], Request.QueryString.Get("Id"));   
            if (!this.IsPostBack)
            {
                //this.presenter.LoadViewFromModel(tblForm.Visible, userInfo.ID);                
            }
            
           
        }

        protected void RaiseClientEvent()
        {
            btnDelete.Attributes.Add("onclick", "return doSetStatus(0);");
            btnAdd.Attributes.Add("onclick", "return AddNew();");
        }

        public IList ConfigList
        {
            set {
                gridObj.DataSource = value;
                gridObj.DataBind();
                this.cList = value;
            }
            get { return this.cList; }
        }

        public String Year
        {
            get { return this.txtYear.Text.Trim(); }
            set { this.txtYear.Text = value; }
        }

        public String Database
        {
            get { return this.txtDatabase.Text.Trim(); }
            set { this.txtDatabase.Text = value; }
        }

        public String UploadPath
        {
            get { return this.txtUploadPath.Text.Trim(); }
            set { this.txtUploadPath.Text = value; }
        }

        public bool IsValid
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }


        protected void btnOK_Click(object sender, EventArgs e)
        {
            presenter.UpdateObject();
        }

        protected void btnOKC_Click(object sender, EventArgs e)
        {
            this.IsContinues = true;
            presenter.UpdateObject();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.presenter.CancelUpdate();
        }

        protected void gridObj_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gridObj.DataSource = (IList<LookupConfigItem>)this.ConfigList;
            gridObj.PageIndex = e.NewPageIndex;
            gridObj.DataBind();

        }

        protected void gridObj_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                
                // Disabled control 
                foreach (Control c in e.Row.Cells[0].Controls)
                {
                    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.PagerTable"))
                    {
                        Table tbl = (Table)c;
                        TableCell cell = new TableCell();
                        cell.Text = "Trang ";
                        cell.HorizontalAlign = HorizontalAlign.Right;
                        tbl.Rows[0].Cells.AddAt(0, cell);
                    }
                }
            }
        }

        // For Delete button
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string ids = "";
            foreach (GridViewRow row in gridObj.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                if (cb.Checked)
                {
                    ids +=(ids == "" ? "" : ",") + Convert.ToString(gridObj.DataKeys[row.RowIndex].Value);
                }
            }
            if (ids != "")
            {
                this.presenter.DeleteObject(ids);
            }
        }


        #region IUserGroupView Members


        public bool IsRole
        {
            get
            {
                return this.isRole;
            }
            set
            {
                this.isRole = value;
                this.btnAdd.Visible = value;
                this.btnDelete.Visible = value;
            }
        }

        #endregion
    }
}